/*
 *   Copyright 2012, Thomas Kerber
 *
 *   Licensed under the Apache License, Version 2.0 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 */
package milk.jpatch.code;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import milk.jpatch.CPoolMap;

import org.apache.bcel.generic.InstructionHandle;
import org.apache.bcel.generic.InstructionList;
import org.apache.bcel.generic.ReturnInstruction;

/**
 * Denotes some code being added somewhere.
 * @author Thomas Kerber
 * @version 1.0.0
 */
public class CodeAddDelta implements CodeDelta{
    
    private static final long serialVersionUID = -4717203407267153716L;
    
    /**
     * Denotes a series of positions to add the code at.
     * @author Thomas Kerber
     * @version 1.0.0
     */
    public static interface AddPositions extends Serializable{
        
        /**
         * 
         * @param il The instruction list to patch.
         * @return The individual positions to add the code at.
         */
        public List<AddPosition> getPositions(InstructionList il);
        
    }
    
    /**
     * Denotes a position where to add something in an instruction list.
     * @author Thomas Kerber
     * @version 1.0.0
     */
    public static abstract class AddPosition implements AddPositions{
        
        private static final long serialVersionUID = 3385551509534646229L;
        
        @Override
        public List<AddPosition> getPositions(InstructionList il){
            return Collections.singletonList(this);
        }
        
        /**
         * 
         * @param il The instruction list to patch.
         * @return The position to insert at (as instruction handle).
         */
        public abstract InstructionHandle getPos(InstructionList il);
        
        /**
         * 
         * @param il The instruction list to patch.
         * @return Whether to append or prepend the code.
         */
        public abstract boolean append(InstructionList il);
    }
    
    /**
     * Denotes code being appended.
     * @author Thomas Kerber
     * @version 1.0.0
     */
    public static class Append extends AddPosition{
        
        private static final long serialVersionUID = -128707975791699269L;
        
        @Override
        public InstructionHandle getPos(InstructionList il){
            return il.getEnd();
        }
        
        @Override
        public boolean append(InstructionList il){
            return true;
        }
    }
    
    /**
     * Denotes code being prepended.
     * @author Thomas Kerber
     * @version 1.0.0
     */
    public static class Prepend extends AddPosition{
        
        private static final long serialVersionUID = 1319258722950235066L;
        
        @Override
        public InstructionHandle getPos(InstructionList il){
            return il.getStart();
        }
        
        @Override
        public boolean append(InstructionList il){
            return false;
        }
        
    }
    
    /**
     * Denotes code being inserted at a predefined position.
     * @author Thomas Kerber
     * @version 1.0.0
     */
    public static class FixedPosition extends AddPosition{
        
        private static final long serialVersionUID = -6338805379218049314L;
        
        /**
         * The return value of getPos()
         */
        private InstructionHandle ih;
        /**
         * The return value of append()
         */
        private boolean append;
        
        /**
         * 
         * @param ih The return value of getPos()
         * @param append The return value of append()
         */
        public FixedPosition(InstructionHandle ih, boolean append){
            this.ih = ih;
            this.append = append;
        }
        
        @Override
        public InstructionHandle getPos(InstructionList il){
            return ih;
        }
        
        @Override
        public boolean append(InstructionList il){
            return append;
        }
        
    }
    
    /**
     * Denotes all positions immediately *before* a return statement.
     * @author Thomas Kerber
     * @version 1.0.0
     */
    public static class BeforeReturn implements AddPositions{
        
        private static final long serialVersionUID = 4591204662726526867L;
        
        @Override
        public List<AddPosition> getPositions(InstructionList il){
            List<AddPosition> ret = new ArrayList<AddPosition>();
            for(InstructionHandle ih : il.getInstructionHandles()){
                if(ih.getInstruction() instanceof ReturnInstruction)
                    ret.add(new FixedPosition(ih, false));
            }
            return ret;
        }
        
    }
    
    /**
     * The positions to add the code at.
     */
    private AddPositions pos;
    /**
     * The code to add.
     */
    private InstructionList toAdd;
    
    /**
     * 
     * @param aps The positions to add the code at.
     * @param toAdd The code to add.
     */
    public CodeAddDelta(AddPositions aps, InstructionList toAdd){
        pos = aps;
        this.toAdd = toAdd;
    }
    
    @Override
    public InstructionList patch(InstructionList il, CPoolMap map){
        InstructionList toAdd = map.applyTo(this.toAdd);
        for(AddPosition ap : pos.getPositions(il)){
            if(ap.append(il))
                il.append(ap.getPos(il), toAdd);
            else
                il.insert(ap.getPos(il), toAdd);
        }
        return il;
    }
    
}
